package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import pojo.Products;
import service.AdminProductsService;
import service.CartService;
import service.UserCenterService;
import util.CommonsUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
@Controller
public class Cart {
    @Autowired
    private AdminProductsService adminProductsService;
    @Autowired
    private CartService cartService;
    @Autowired
    private UserCenterService userCenterService;
    @RequestMapping("/searchAllProducts")
    public String searchAllProducts(Model model){
        List<Products> product=adminProductsService.sAP();
        model.addAttribute("product",product);
        return "searchAllProduct";
    }
    @RequestMapping("/toCart")
    public String addCart(HttpSession session,HttpServletRequest request) throws ParseException {
        String id = request.getParameter("pid");
        String count = request.getParameter("pcount");
        String price = request.getParameter("pprice");
        String startDate = request.getParameter("startdate");
        String endDate = request.getParameter("enddate");
        System.out.println("开始日期："+startDate+""+"结束日期："+endDate);
        Date start = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
        Date star = new SimpleDateFormat("yyyy-MM-dd").parse(startDate);
        Date end = new SimpleDateFormat("yyyy-MM-dd").parse(endDate);
        System.out.println("开始日期："+start+""+"结束日期："+end);
        Date nextDay=start;
        int i=0;
        while (nextDay.before(end)){
            Calendar cld = Calendar.getInstance();
            cld.setTime(start);
            cld.add(Calendar.DATE,1);
            start = cld.getTime();
            nextDay = start;
            i++;
        }
        double total = Double.parseDouble(count)*Double.parseDouble(price)*i;
        System.out.println("总价格为："+total);
        System.out.println("相差天数为："+i);
        int uid = CommonsUtils.getUserId(session);
        pojo.Cart cart = new pojo.Cart();
        cart.setPid(Integer.parseInt(id));
        cart.setUid(uid);
        cart.setCount(Integer.parseInt(count));
        cart.setTotal(total);
        cart.setStarttime(star);
        cart.setEndtime(end);
        cart.setOrdertime(i);
        int a=cartService.aC(cart);
        if (a>0){
            System.out.println("订购成功！");
            return "cartSuccess";
        }
            System.out.println("订购失败！");
        return "searchAllProducts";
    }
    @RequestMapping("/putCart")
    public String putCart(Integer pid,Model model){
        List<Products> p = adminProductsService.sOP(pid);
        model.addAttribute("p",p);
        return "userPutCart";
    }
    @RequestMapping("/delC")
    public String delCart(Integer cid,Model model,HttpSession session){
        int uid = CommonsUtils.getUserId(session);
        int a= cartService.dC(cid);
        if (a>0){
            System.out.println("购物车删除成功！");
            List<pojo.Cart> carts=userCenterService.sMC(uid);
            model.addAttribute("cart",carts);
            return "userSelectMyCartPage";
        }
        System.out.println("购物车删除失败！");
        return "userSelectMyCartPage";
    }
}
